package com.lanxin.oa.personnel.controller;
import com.lanxin.bean.JcBasicdatavalue;
import com.lanxin.bean.ZzDepartment;
import com.lanxin.bean.ZzEmployee;
import com.lanxin.oa.personnel.service.DeptService;
import com.lanxin.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Controller
@RequestMapping("/dept")
public class DeptController {
    @Autowired
    private DeptService deptService;

    @RequestMapping("/selectDept")
    public ModelAndView selectDept(Integer page,Integer pageSize,ZzDepartment dept){
        PageBean<ZzDepartment> pageBean = deptService.getDeptPage(page, pageSize, dept);
        ModelAndView mv=new ModelAndView();
        List<ZzDepartment> listDept = deptService.listDeptName();
        mv.addObject("pageBean",pageBean);
        mv.addObject("dept",dept);
        mv.addObject("listDept",listDept);
        mv.setViewName("../view/personnel/listdept.jsp");
        return mv;
    }

    @RequestMapping("/listDeptUseSelect")
    @ResponseBody
    public List<ZzDepartment> listDeptUseSelect(){
       List<ZzDepartment> listdept = deptService.listDeptName();
        return listdept;
    }

    @RequestMapping("/listDeptJobUseSelect")
    @ResponseBody
    public Set<JcBasicdatavalue> listDeptJobUseSelect(Integer deptid){
        ZzDepartment d = deptService.deptJob(deptid);
        if(d!=null){
            return d.getRoles();
        }
        return null;
    }

    @RequestMapping("/listEmpUseSelete")
    @ResponseBody
    public List<ZzEmployee> listEmpUseSelete(Integer deptid,Integer jobid){
        List<ZzEmployee> listEmps = deptService.listEmpName(deptid,jobid);
        return listEmps;
    }


    @RequestMapping("/toAddDept")
    public ModelAndView toAddDept(){
        ModelAndView mv=new ModelAndView();
        List<ZzDepartment> listDeptNames =deptService.selectDeptService();
        List<JcBasicdatavalue> listJobs =deptService.selectJobs();
        mv.addObject("listDeptNames",listDeptNames);
        mv.addObject("listJobs",listJobs);
        mv.setViewName("../view/personnel/editdept.jsp");
        return mv;
    }

    @RequestMapping("/AddDept")
    public ModelAndView AddDept(ZzDepartment dept,Integer[] jobsid){
        ModelAndView mv=new ModelAndView();
        if (dept.getOid() == null) {// 添加部门

            deptService.insertDept(dept,jobsid);

        } else {// 修改部门
           deptService.updateDept(dept,jobsid);
        }
        mv.setViewName("redirect:/dept/selectDept");// 重定向到查询方法里
        return mv;
    }

    @RequestMapping("/deptInfo")
    public ModelAndView deptInfo(Integer oid){
        ModelAndView mv=new ModelAndView();
         ZzDepartment dept=deptService.deptById(oid);
         mv.addObject("dept",dept);
        mv.setViewName("../view/personnel/deptinfo.jsp");
        return mv;
    }

    @RequestMapping("/toUpdateDept")
    public ModelAndView toUpdateDept(Integer oid){
        ModelAndView mv=new ModelAndView();
        ZzDepartment dept=deptService.deptById(oid);
        List<ZzDepartment> listDeptNames =deptService.selectDeptService();
        List<JcBasicdatavalue> listJobs =deptService.selectJobs();
        mv.addObject("listDeptNames",listDeptNames);
        mv.addObject("listJobs",listJobs);
        mv.addObject("dept",dept);
        mv.setViewName("../view/personnel/editdept.jsp");
        return mv;
    }

    @RequestMapping("/checkDeptName")
    @ResponseBody
    public Map<String,Boolean> checkDeptName(String deptName,Integer oid){
        Map<String,Boolean> map=new HashMap<String, Boolean>();
        boolean b=deptService.selectByNameService(deptName,oid);
        if(!b){
            map.put("valid", true);
            return map;
        }
        map.put("valid", false);
        return map;
    }

    @RequestMapping("/deleteDept")
    @ResponseBody
    public boolean deleteDept(ZzDepartment dept){
        boolean b = deptService.delete(dept);
        return b;
    }
}
